package com.mk.legoscans2.activities;

import java.util.List;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.mk.legoscans2.LegoScans3Application;
import com.mk.legoscans2.R;
import com.mk.legoscans2.Scan;
import com.mk.legoscans2.actionbar.ActionBarActivity;

public class SearchActivity extends ActionBarActivity {

	private ScanListFragment _scanListFragment;
	private EditText _searchField;
	private FragmentActivity _activity;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);
		
		_activity = this;

		setSearchTitle(null);
		
		_scanListFragment = (ScanListFragment)getSupportFragmentManager().findFragmentById(R.id.search_scan_list_fragment_container);
		_scanListFragment.updateList();
		
		_searchField = (EditText)findViewById(R.id.searchField);
		_searchField.setText(LegoScans3Application.SearchQuery);
		
		toggleSoftInput(LegoScans3Application.SearchQuery == null || LegoScans3Application.SearchQuery.trim().equals(""));
		
		Button searchButton = (Button)findViewById(R.id.searchButton);
		searchButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(getSearchQuery() != null && getSearchQuery().length() >= 3)
					new SearchScansTask().execute();
				else
					giveFeedback(getResources().getString(R.string.search_feedback_message));
			}
		});
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		LegoScans3Application.SearchQuery = getSearchQuery();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		if(LegoScans3Application.TempScans != null && _scanListFragment != null)
		{
			_scanListFragment.updateList();
			setSearchTitle(LegoScans3Application.TempScans.size());
		}
	}
	
	private String getSearchQuery() {
		return _searchField.getText().toString().trim();
	}
	
	private void toggleSoftInput(boolean show)
	{
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		if(show)
			imm.showSoftInputFromInputMethod(_searchField.getWindowToken(), 0);
		else
			imm.hideSoftInputFromWindow(_searchField.getWindowToken(), 0);
	}
	
	private void setSearchTitle(Integer count)
	{
		if(count == null)
			_activity.setTitle(getResources().getString(R.string.search_default_title));
		else
			_activity.setTitle(String.format(getResources().getString(R.string.search_title), count, count == 1 ? "" : "s"));
	}
	
	private void giveFeedback(String message)
	{
		Toast.makeText(_activity, message, 1000).show();
	}
	
	private class SearchScansTask extends AsyncTask<String, Void, List<Scan>> {

		@Override
		protected void onPreExecute() {
			_scanListFragment.setIsLoading(true);
			toggleSoftInput(false);
		}
		
		protected List<Scan> doInBackground(String... params) {
			return LegoScans3Application.ApiRepository.getScansByQuery(getSearchQuery());
		}

		protected void onPostExecute(List<Scan> result) {
			LegoScans3Application.TempScans = result;
			_scanListFragment.updateList();
			if(LegoScans3Application.TempScans != null)
				setSearchTitle(LegoScans3Application.TempScans.size());
			else
				giveFeedback(getResources().getString(R.string.generic_network_error_message));
		}
	}
}
